#!/bin/bash

##by CC
set -e
src=/usr/local/src
nginxname=nginx-1.13.0
cd $src
echo "解压缩"
tar zxvf /usr/local/src/`echo $nginxname`.tar.gz > /dev/null
cd $src/$nginxname
echo "编译"
/usr/local/src/$nginxname/configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-file-aio --with-http_v2_module --with-ipv6 > /dev/null
echo $?
echo "make"
make > /dev/null
echo $?
echo "make install"
make install > /dev/null
echo $?
echo "查看是否编译成功"

tee /usr/lib/systemd/system/nginx.service <<-EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s QUIT \$MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

mkdir -p /var/cache/nginx/client_temp
chmod +x /usr/lib/systemd/system/nginx.service
useradd -s /sbin/nologin nginx
/usr/local/nginx/sbin/nginx -t
systemctl enable nginx.service
systemctl start nginx.service
